#include "stm32f4xx_syscfg.h"
#include "stm32f4xx_gpio.h"


#include "sys.h"
#include "usart.h"
#include "delay.h"
#include "LED.h"
#include "printf.h"

// 1- redirecting printf(). adding "printf.h" and "printf.c"
// ***** GitHub repossitory: A printf / sprintf Implementation for Embedded Systems *****
// 2- rewriting fputc(), on which printf() depends on
// 3- HSE_VALUE is set to 8 MHz instead of 25 MHz in stm32f4xx.h


int main(void)
{

	u8 t;
	u8 len;
	u16 times=0;
	int i = 1;
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
	delay_init(168);
	uart_init(115200);
	LED_Init();
	while(1)
	{
		if(USART_RX_STA&0x8000)
		{
			len=USART_RX_STA&0x3fff;//得到此次接收到的数据长度
			printf("\r\nyour message is:\r\n");
			for(t=0;t<len;t++)
			{
				USART_SendData(USART1, USART_RX_BUF[t]);         //向串口1发送数据
				while(USART_GetFlagStatus(USART1,USART_FLAG_TC)!=SET);//等待发送结束
			}
			printf("\r\n");
			USART_RX_STA=0;
		}else
		{
			times++;
			if(times%20==0){printf("please type in ... (%d)\r\n",i++);}
			delay_ms(50);
		}
		if(i > 255) i = 1;
	}
}
